//
//  VipAddressController.m
//  CenturyCity
//
//  Created by nsm on 2019/6/12.
//  Copyright © 2019 beisheng. All rights reserved.
//

#import "VipAddressController.h"

@interface VipAddressCell : NSMTableViewCell
@property (nonatomic, strong) UILabel *addrLabel;
@end

@implementation VipAddressCell
- (void)setObject:(id)obj {
    NSString *highlightedStr = @" VIP ";
    NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:highlightedStr];
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineSpacing = 1;
    paragraph.alignment = NSTextAlignmentLeft;
    [attrM addAttribute:NSParagraphStyleAttributeName
                  value:paragraph
                  range:NSMakeRange(0, highlightedStr.length)];
    // VIP高亮加背景
    [attrM addAttributes:@{NSBackgroundColorAttributeName: GROUP_TABLECOLOR,
                           NSForegroundColorAttributeName: UIColor.blueColor,
                           NSFontAttributeName: BoldSystemFont(12)}
                   range:NSMakeRange(0, highlightedStr.length)];
    [attrM appendAttributedString:AttributedString(FormatString(@" %@", obj[@"title"]),
                                                   SystemFont(14),
                                                   COLOR_0,
                                                   FormatString(@"\n%@",obj[@"address"]),
                                                   CustomFont(14),
                                                   SUB_TEXT_COLOR,
                                                   NSTextAlignmentLeft,
                                                   1)];
    _addrLabel.attributedText = attrM;
}

- (void)setupSubviews {
    MyLinearLayout *layout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    layout.myHorzMargin = 0;
    layout.wrapContentHeight = YES;
    layout.padding = UIEdgeInsetsMake(12, 15, 12, 12);
    layout.subviewHSpace = 12;
    layout.gravity = MyGravity_Vert_Center;
    layout.cacheEstimatedRect = YES;
    [self.contentView addSubview:layout];
    self.rootLayout = layout;
    
    UILabel *label = UILabel.new;
    label.weight = 1;
    label.wrapContentHeight = YES;
    label.font = CustomFont(14);
    label.textColor = SUB_TEXT_COLOR;
    [layout addSubview:label];
    _addrLabel = label;
}
@end


@implementation VipAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTableView];
}

- (void)initTableView {
    self.tableView.size = CGSizeMake(kScreenWidth, kScreenHeight-kNavBarHeight);
    self.tableView.separatorColor = SEPARATOR_COLOR;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    self.tableView.estimatedRowHeight = 64;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.tableHeaderView = [self viewForHeader];
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    self.tableView.urlString = @"common/getVipAddressList";
    self.tableView.parameters = nil;
    self.tableView.headerRefreshEnabled = YES;
    self.tableView.footerRefreshEnabled = YES;
    self.tableView.emptySetEnabled = YES;
    self.tableView.emptySetShouldAllowScroll = YES;
    
    MJRefreshAutoNormalFooter *footerRefresh = (MJRefreshAutoNormalFooter *)self.tableView.mj_footer;
    [footerRefresh setTitle:@"--- 更多vip地址接入中, 敬请期待 ---" forState:MJRefreshStateNoMoreData];
    
    self.tableView.sourceManager.classForCellAtIndexPath = ^Class(NSIndexPath *indexPath) {
        return VipAddressCell.class;
    };
    @nsm_weak(self)
    self.tableView.didRefreshingHeaderSuccess = ^(id responseObject) {
        @nsm_strong(self)
        id data = ((NSMResponseObject *)responseObject).data;
        self.tableView.sourceManager.dataArray = [NSMutableArray arrayWithArray:SafeArray(data[@"list"])];
    };
    self.tableView.didRefreshingFooterSuccess = ^(id responseObject) {
        @nsm_strong(self)
        id data = ((NSMResponseObject *)responseObject).data;
        [self.tableView.sourceManager.dataArray addObjectsFromArray:data[@"list"]];
    };
    self.tableView.didRefreshingHeaderEmpty = ^(id responseObject) {
        @nsm_strong(self)
        self.tableView.sourceManager.dataArray = @[].mutableCopy;
        self.tableView.imageForEmptySet = kEmptyDataImage;
        self.tableView.titleForEmptySet = kEmptyDataDesc(((NSMResponseObject *)responseObject).desc);
    };
}

- (UIView *)viewForHeader {
    NSMButton *btn = NSMButton.new;
    btn.backgroundColor = COLOR_4;
    btn.size = CGSizeMake(kScreenWidth, 44);
    btn.titleLabel.font = CustomFont(14);
    btn.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 15);
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [btn setTitleColor:COLOR_0 forState:UIControlStateNormal];
    btn.title = @"其他";
    [btn addTarget:self action:@selector(onClickOther:) forControlEvents:UIControlEventTouchUpInside];
    
    ViewBorder(btn, kSinglePixel, SEPARATOR_COLOR);
    
    return btn;
}

- (void)onClickOther:(id)sender {
    !self.onClickOther?:self.onClickOther();
}

@end
